import {userLogin, userLogout} from '@/service/user_serv.js' 
export default {
	/**
	 * 用户模块
	 */
	namespaced: true,
	state: {
		token: '',
		userinfo: ''
	},
	getters: {
		isTokenValid: state => !!state.token
	},
	mutations: {
		SET_TOKEN(state, {token}) {
			state.token = token
			uni.setStorageSync('token', token)
		},
		REMOVE_TOKEN(state) {
			state.token = ''
			uni.removeStorageSync('token')
		},
		SET_USER_INFO(state, info) {
			state.userinfo = info
		}
	},
	actions: {
		// 用户登录
		userLogin({commit}, {mobile, password}) {
			return userLogin(mobile, password).then(res => {
				if(res.code === 0) {
					let token = res.data.token
					let userInfo = res.data
					commit('SET_TOKEN', {token})
					commit('SET_USER_INFO', userInfo)
				}
			})
		},
		
		// 用户退出
		userLogout({commit}) {
			return userLogout().then(res => {
				if(res.code === 0){
					commit('REMOVE_TOKEN', {token: ''})
					commit('SET_USER_INFO', {})
					uni.removeStorageSync('token')
				}
			})
		}
	}
}